package com.xuecheng.content.infrastructure.util;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XueChengPlusException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-09-07  11:18
 */
@Slf4j
public class SecurityUtil {

    /**
     * 获取用户信息
     * @return
     */
    public static User getUser(){
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            if (principal instanceof String){
                return JSON.parseObject(principal.toString(), User.class);
            } else {
                return null;
            }
        } catch (Exception e){
            log.error("获取token用户信息失败，{}", e.getMessage());
            throw new XueChengPlusException("获取token用户信息失败");
        }
    }


    @Data
    public static class User implements Serializable {
        private static final long serialVersionUID = 1L;

        private Long id;

        private String username;

        private String password;

        private String salt;

        private String name;
        private String nickname;
        private String wxUnionid;
        private String companyId;
        /**
         * 头像
         */
        private String userpic;

        private String utype;

        private LocalDateTime birthday;

        private String sex;

        private String email;

        private String cellphone;

        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;

    }
}
